package com.l024.easyandroidlib.manager;

import android.app.Activity;

import java.util.Stack;

public class ActivityManager {

    private Stack<Activity> activities;

    private static ActivityManager _instance;

    public static ActivityManager getInstance() {
        if (_instance == null) {
            _instance = new ActivityManager();
        }
        return _instance;
    }

    public void addActivity(Activity activity) {
        if (activities == null) {
            activities = new Stack<>();
        }
        if (activities.search(activity) == -1) {
            activities.push(activity);
        }
    }

    public void removeActivity(Activity activity) {
        if (activities != null && activities.size() > 0 && activities.search(activity) != -1) {
            activities.remove(activity);
        }
    }

    public void removeAll() {
        if (activities != null && activities.size() > 0) {
            while (!activities.empty()) {
                Activity mActivity = activities.pop();
                if (mActivity != null) {
                    mActivity.finish();
                }
            }
        }
    }

    public void Exit() {
        if (activities != null && activities.size() > 0) {
            while (!activities.empty()) {
                Activity mActivity = activities.pop();
                if (mActivity != null) {
                    mActivity.finish();
                }
            }
        }
        System.exit(0);
    }
}
